<?php


namespace App\Http\Controllers\Wechat;


use App\Http\Controllers\Controller;
use App\Models\Area;
use App\Models\Community;
use App\Services\CategoryService;
use App\Services\HouseService;
use App\Services\NavService;
use App\Services\UserService;
use App\Services\WechatService;
use Illuminate\Support\Facades\Request;

class PublicController extends Controller
{


    /**
     * Notes: 根据code获取session的信息
     * User: lstink
     * Date: 2021/3/1
     * Time: 5:25 下午
     * @param WechatService $wechat_service
     * @param UserService $user_service
     * @param $code
     * @return array
     */
    public function getUserInfoByCode(WechatService $wechat_service, UserService $user_service, $code): array
    {
        $session_info = $wechat_service->getSessionInfo($code);
        if (!isset($session_info['openid'])) {
            return $this->error($session_info['errmsg']);
        }
        $user_info = $user_service->checkUserIsExistsByOpenid($session_info);
        if ($user_info) {
            //用户登录，返回token
            $info = $user_service->wechatAuth($user_info);
            return $info['status'] ? $this->success($info['data']) : $this->error($info['msg']);
        }

        return $this->auto(config('responseCode.register'), '请填写注册信息', ['openid' => $session_info['openid'], 'unionid' => $session_info['unionid']]);
    }

    /**
     * 获取省市区数据
     * @param Area $area
     * @return array
     * @author ypp
     * @date 2020/03/03
     */
    public function areaLists(Area $area)
    {
        $type = request()->type??2;
        $otherIds = [];
        //注册页的省市区只查与小区相关的数据
        if($type==1){
            $provinceIds = Community::where(['is_deleted'=>0,'status'=>1])->pluck('province')->toArray();
            $area = $area->whereIn('id',array_unique($provinceIds));
        }
        $lists = $area->with('children')
            ->where('pid',0)
            ->select('id as value','name as label','code','pid')->get()->toArray();
        return $this->success($lists);
    }

    /**
     * 获取小区列表
     * @param HouseService $houseService
     * @param $id
     * @return array
     * @author ypp
     * @date 2020/03/03
     */
    public function allCommunity(HouseService $houseService, $province_id,$cid,$areaid)
    {
        return $this->success($houseService->getAllCommunity($province_id,$cid,$areaid));
    }
    /**
     * 获取小区下的所有楼栋
     * @param HouseService $houseService
     * @param $id
     * @return array
     * @author ypp
     * @date 2020/03/03
     */
    public function getAllHouseByCommunity(HouseService $houseService, $id)
    {
        return $this->success($houseService->getHouseListByCommunity($id));
    }
    /**
     * 获取楼栋下的所有单元
     * @param HouseService $houseService
     * @param $id
     * @return array
     * @author ypp
     * @date 2020/03/03
     */

    public function getAllElementByHouse(HouseService $houseService, $id)
    {
        return $this->success($houseService->getElementListByHouse($id));
    }
    /**
     * 获取楼栋下的所有楼层
     * @param HouseService $houseService
     * @param $id
     * @return array
     * @author ypp
     * @date 2020/03/03
     */
    public function getAllFloorByElement(HouseService $houseService, $id)
    {
        return $this->success($houseService->getFloorListByElement($id));
    }


    /**
     * 获取楼栋下的所有楼层
     * @param HouseService $houseService
     * @param $id
     * @return array
     * @author ypp
     * @date 2020/03/03
     */
    public function getAllRoomByFloor(HouseService $houseService, $id)
    {
        return $this->success($houseService->getRoomListByFloor($id));
    }

    /**
     * @Note 获取导航
     * @param NavService $NavService
     * @return array
     * @author ypp
     * @date 2021/3/5
     * @time 15:18
     */
    public function getNavList(NavService $NavService)
    {
        $rs = $NavService->getNavList();
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    /**
     * @Note 获取类目
     * @param CategoryService $CategoryService
     * @return array
     * @author ypp
     * @date 2021/3/5
     * @time 15:18
     */
    public function getIndexCateList(CategoryService $CategoryService)
    {
        $rs = $CategoryService->getList();
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    public function register(Request $request)
    {

    }

}
